/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUIMotorista.java
 *
 * Created on 23/11/2011, 00:20:08
 */
package GUI;
import tp1POO.Console;
import tp1POO.Verifica;
import tp1POO.Persistencia.GerenciadorObjetos;
import tp1POO.Persistencia.Pesquisa;
import tp1POO.Modelo.*;
import GUI.GUIErro;
import javax.swing.JOptionPane;
import tp1POO.GestorLog;
/**
 *
 * @author Fernando
 */
public class GUIMotorista extends javax.swing.JFrame {
    private String msg="";
    private int acesso=0;

    /** Creates new form GUIMotorista */
    public GUIMotorista() {
        initComponents();
    }

    public GUIMotorista(int a) {
        initComponents();
        this.acesso = a;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */



    private boolean erroMotorista () {
        boolean erro=false;
        this.msg = "";

        erro = !Verifica.nomeMotorista(campoNome.getText()).isEmpty() || !Verifica.cpfMotorista(campoCpf.getText()).isEmpty()||campoTel.getText().isEmpty()||campoEnd.getText().isEmpty();
        if (campoTel.getText().isEmpty()) {
            msg = msg + "ERRO : Telefone Inválido!\n";
        }
        if (campoEnd.getText().isEmpty()) {
            msg = msg + "ERRO : Endereço Inválido!\n";
        }
        msg = msg + Verifica.nomeMotorista(campoNome.getText()) + Verifica.cpfMotorista(campoCpf.getText());
        if (erro) {
            JOptionPane.showMessageDialog(jPMotorista,msg, "Error", JOptionPane.ERROR_MESSAGE);
        }

        return erro;
    }


    private void addMotorista () {
        Motorista mot = null;
        String nome="",cpf="",cnh="";

       nome = campoNome.getText();
       cpf = campoCpf.getText();
       cnh = campoCnh.getText();
       mot = new Motorista(nome, cpf, cnh);
       mot.setEndereco(campoEnd.getText());
       mot.setTelefone(campoTel.getText());
       GerenciadorObjetos.obterInstancia().salvarObj(mot);
       GestorLog.obterInstancia().inserirMsgLog(GuiCreator.obterInstancia().getGuiFuncionalidades().getFuncionario().getCpf()+" - Motorista cadastrado com sucesso!\n\tNome : "+
               mot.getNome()+"\n\tCPF : "+mot.getCpf()+"\n\tCNH : "+mot.getCNH());
       campoNome.setText("");
       campoTel.setText("");
       campoCpf.setText("");
       campoEnd.setText("");
       campoCnh.setText("");
       JOptionPane.showMessageDialog(jPMotorista,"Motorista cadastrado com Sucesso!", "Sucesso", JOptionPane.INFORMATION_MESSAGE);
    }













    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPMotorista = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        campoNome = new javax.swing.JTextField();
        campoCpf = new javax.swing.JTextField();
        campoCnh = new javax.swing.JTextField();
        campoTel = new javax.swing.JTextField();
        campoEnd = new javax.swing.JTextField();
        jBCancelar = new javax.swing.JButton();
        jBOk = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Cadastrar Motorista");

        jPMotorista.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Dados do Motorista", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 1, 12))); // NOI18N

        jLabel1.setText("Nome:");

        jLabel2.setText("CPF:");

        jLabel3.setText("CNH:");

        jLabel4.setText("Endereço:");

        jLabel6.setText("Telefone:");

        campoCpf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoCpfActionPerformed(evt);
            }
        });

        campoCnh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoCnhActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPMotoristaLayout = new javax.swing.GroupLayout(jPMotorista);
        jPMotorista.setLayout(jPMotoristaLayout);
        jPMotoristaLayout.setHorizontalGroup(
            jPMotoristaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPMotoristaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPMotoristaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPMotoristaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPMotoristaLayout.createSequentialGroup()
                            .addGap(6, 6, 6)
                            .addComponent(jLabel6))
                        .addGroup(jPMotoristaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)))
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPMotoristaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPMotoristaLayout.createSequentialGroup()
                        .addComponent(campoCpf, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoCnh, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE))
                    .addComponent(campoNome, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)
                    .addComponent(campoEnd, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)
                    .addComponent(campoTel, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPMotoristaLayout.setVerticalGroup(
            jPMotoristaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPMotoristaLayout.createSequentialGroup()
                .addContainerGap(24, Short.MAX_VALUE)
                .addGroup(jPMotoristaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(campoNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPMotoristaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(campoCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(campoCnh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPMotoristaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(campoEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPMotoristaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(campoTel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jBCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/icoErro.gif"))); // NOI18N
        jBCancelar.setText("Fechar");
        jBCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBCancelarActionPerformed(evt);
            }
        });

        jBOk.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/certo.gif"))); // NOI18N
        jBOk.setText("OK");
        jBOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBOkActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPMotorista, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jBOk, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jBCancelar)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jBCancelar, jBOk});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPMotorista, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBOk)
                    .addComponent(jBCancelar))
                .addContainerGap(34, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jBCancelar, jBOk});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void campoCnhActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoCnhActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_campoCnhActionPerformed

    private void campoCpfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoCpfActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_campoCpfActionPerformed

    private void jBOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBOkActionPerformed
        if (!erroMotorista()) {
            addMotorista();
             if (this.acesso != 0) {
                   GuiCreator.obterInstancia().getGuiLocacoao().carregaListaMotoristas();
             }
        }
    }//GEN-LAST:event_jBOkActionPerformed

    private void jBCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBCancelarActionPerformed
        GestorLog.obterInstancia().inserirMsgLog("Funcionalidade de Adicionar motorista foi fechada("+GuiCreator.obterInstancia().getGuiFuncionalidades().getFuncionario().getCpf()+")");
        GuiCreator.obterInstancia().removerGuiMotorista();
    }//GEN-LAST:event_jBCancelarActionPerformed




















    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new GUIMotorista().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField campoCnh;
    private javax.swing.JTextField campoCpf;
    private javax.swing.JTextField campoEnd;
    private javax.swing.JTextField campoNome;
    private javax.swing.JTextField campoTel;
    private javax.swing.JButton jBCancelar;
    private javax.swing.JButton jBOk;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPMotorista;
    // End of variables declaration//GEN-END:variables
}
